<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/26
 * Time: 13:17
 */

namespace Admin\Controller;
use Think\Controller;

class BaseController extends Controller {

    public function __construct()
    {
        parent::__construct();
        if(empty($_SESSION['user']))
        {
            redirect(U('admin.php/login/login'));
        }
    }

    public function get_city_list()
    {
        $model = D('region');
    }

    public function imageinfo($url)
    {
//        $url = 'http://odsiqjn8i.bkt.clouddn.com/2016-10-26_58106676ccb6c.jpg?imageInfo';
        $result = $this->get($url.'?imageInfo');
        $res = json_decode($result,true);
        $data = array(
            'width'=>$res['width'],
            'height'=>$res['height']
        );
//        var_dump($data);
        return $data;
    }

    //curl 发送get请求
    public function get($url)
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }

    /*
   * 将图片加水印模拟上传提交到七牛云
   * @param string $path 临时文件的路径
   * @param string $user_id 用户的id
   * @param string $type 上传的图片类型
   * @param string $img 将图片进行加水印
   */
    function upload_curl_pic($path,$user_id,$type)
    {
        $image = new \Think\Image();
// 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
        $logo = "./Public/images/upload/logo.png";
        $number = rand(1000,9999);
        $cutter = "./Public/images/upload/".$user_id.$number."thumb.jpg";
        $save = "./Public/images/upload/".$user_id.$number.".jpg";//设置水印图片路径
        $image->open($path);
        $image->thumb(1080, 1920,\Think\Image::IMAGE_THUMB_SCALE)->save($cutter);
        $image->open($cutter)->water($logo,\Think\Image::IMAGE_WATER_SOUTHEAST)->save($save);
        $url  = 'http://www.hzparty.com/index.php/base/qiniu';//服务器上的地址
//        $url  = 'http://127.0.0.1/svnwelove/index.php/base/qiniu';//本地地址

        $fields = array(
            "user_id"=>$user_id,
            "type"=>$type,
            "file"=>"@".$save
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
//        var_dump($result);
        return $result;
    }

    /*
     * 将图片上传到七牛并进行写入数据库
     * @param string $user_id 用户id
     * @param string $type 图片类型
     * @param string $url 带水印图片上传到七牛之后获取到的图片url
     */
    public function qiniu()
    {
        $user_id = I('post.user_id');
        $type = I('post.type');
        $model = D('images');
        $setting=C('UPLOAD_SITEIMG_QINIU');
        $Upload = new \Think\Upload($setting);
        $info = $Upload->upload($_FILES);
        $url = $info['file']['url'];
        $image = $this->imageinfo($url);
        $data = array(
            'user_id'=>$user_id,
            'pic_url'=>$url,
            'width'=>$image['width'],
            'height'=>$image['height'],
            'type'=>$type,
            'up_time'=>date('Y-m-d H:i:s',time()),
        );
        $model->data($data)->add();
    }

    /*
  * 将图片加水印模拟上传提交到七牛云
  * @param string $path 临时文件的路径
  * @param string $user_id 用户的id
  * @param string $img 将图片进行加水印
  */
    function upload_header_pic($path,$user_id)
    {
        $image = new \Think\Image();
// 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
        $logo = "./Public/images/upload/logo.png";
        $number = rand(1000,9999);
        $cutter = "./Public/images/upload/".$user_id.$number."thumb.jpg";
        $save = "./Public/images/upload/".$user_id.$number.".jpg";//设置水印图片路径
        $image->open($path);
        $image->thumb(1080, 1920,\Think\Image::IMAGE_THUMB_SCALE)->save($cutter);
        $image->open($cutter)->water($logo,\Think\Image::IMAGE_WATER_SOUTHEAST)->save($save);
        $url  = 'http://www.hzparty.com/index.php/base/header_qiniu';
//        $url  = 'http://127.0.0.1/svnwelove/index.php/base/header_qiniu';

        $fields = array(
            "user_id"=>$user_id,
            "file"=>"@".$save
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
//        var_dump($result);
        return $result;
    }

    /*
  * 将用户头像上传到七牛云并进行写入数据库
  * @param string $user_id 用户id
  * @param string $url 带水印图片上传到七牛之后获取到的图片url
  */
    public function header_qiniu()
    {
        $model = D('userinfo');
        $user_id = I('post.user_id');
        $setting=C('UPLOAD_SITEIMG_QINIU');
        $Upload = new \Think\Upload($setting);
        $info = $Upload->upload($_FILES);
        $url = $info['file']['url'];
        $data = array(
            'header_pic'=>$url,
        );
        $condition['user_id'] = $user_id;
        $model->where($condition)->save($data);
    }

    /*
    * 根据月份与时间查询星座
    * @param int  $m //查询的月份
    * @param int $d //查询 的日期
    * @return //返回星座信息
    */
    public function getstar($m,$d)
    {
        if ($m == 1) {
            if ($d < 21) {
                return "魔羯座";
            } else {
                return "水瓶座";
            }
        } elseif ($m == 2) {
            if ($d < 20) {
                return "水瓶座";
            } else {
                return "双鱼座";
            }
        } elseif ($m == 3) {
            if ($d < 21) {
                return "双鱼座";
            } else {
                return "白羊座";
            }
        } elseif ($m == 4) {
            if ($d < 21) {
                return "白羊座";
            } else {
                return "金牛座";
            }
        } elseif ($m == 5) {
            if ($d < 22) {
                return "金牛座";
            } else {
                return "双子座";
            }
        } elseif ($m == 6) {
            if ($d < 22) {
                return "双子座";
            } else {
                return "巨蟹座";
            }
        } elseif ($m == 7) {
            if ($d < 23) {
                return "巨蟹座";
            } else {
                return "狮子座";
            }
        } elseif ($m == 8) {
            if ($d < 24) {
                return "狮子座";
            } else {
                return "处女座";
            }
        } elseif ($m == 9) {
            if ($d < 24) {
                return "处女座";
            } else {
                return "天秤座";
            }
        } elseif ($m == 10) {
            if ($d < 24) {
                return "天秤座";
            } else {
                return "天蝎座";
            }
        } elseif ($m == 11) {
            if ($d < 23) {
                return "天蝎座";
            } else {
                return "射手座";
            }
        } elseif ($m == 12) {
            if ($d < 22) {
                return "射手座";
            } else {
                return "魔羯座";
            }
        }
    }
}